﻿#if UNITY_EDITOR
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
using Unity.Collections;
using UnityEngine;
/// <summary>
/// AssetBundle资源包生成
/// </summary>
[XmlRoot("ESF_Xml_BuildAssetBundle")]
public class ESF_Xml_BuildAssetBundle : AbsESF_Xml
{
    /// <summary>
    /// AssetBundle根目录
    /// </summary>
    [XmlElement("AssetBundle根目录")]
    [ESF_GUIOperation()]
    [ESF_PathSelect(enESF_PathSelectCatalogClassify.InternalAssets, enESF_PathSelectAssetClassify.Folder)]
    [ESF_GUISeparator()]
    public string[] assetBundleRoots;


    /// <summary>
    /// SubPackage根目录
    /// </summary>
    [XmlElement("资源下载窗口")]
    [ESF_GUIOperation()]
    public ESF_Xml_DonwLoadWindow_Lang[] FirstDownloadingWindowPath;
    /// <summary>
    /// SubPackage根目录
    /// </summary>
    [XmlElement("SubPackage根目录")]
    [ESF_GUIOperation()]
    [ESF_PathSelect(enESF_PathSelectCatalogClassify.InternalAssets, enESF_PathSelectAssetClassify.Folder)]
    [ESF_GUISeparator()]
    public string[] SubPackageRoots;

    /// <summary>
    /// 缺省语言包
    /// </summary>
    [XmlElement("缺省语言包")]
    [ReadOnly()]
    public ESF_Xml_BuildAssetBundle_Lang defaultAlternateLang;

    /// <summary>
    /// 支持语言包
    /// </summary>
    [XmlElement("支持语言包")]
    [ReadOnly()]
    [ESF_ArrayMaxLength(typeof(enESF_AllLanguageClassify))]
    public ESF_Xml_BuildAssetBundle_Lang[] supportLangs;

    /// <summary>
    /// 支持语言包
    /// </summary>
    [XmlElement("不支持语言包")]
    [ReadOnly()]
    [ESF_ArrayMaxLength(typeof(enESF_AllLanguageClassify))]
    public ESF_Xml_BuildAssetBundle_Lang[] unSupportLangs;

    /// <summary>
    /// OnResolve
    /// </summary>
    protected override void OnResolve()
    {
        List<ESF_Xml_BuildAssetBundle_Lang> sup = new List<ESF_Xml_BuildAssetBundle_Lang>();
        List<ESF_Xml_BuildAssetBundle_Lang> unSup = new List<ESF_Xml_BuildAssetBundle_Lang>();
        if (ESF_Logic.languageSetting.xml.items != null)
        {
            foreach (var key in ESF_Logic.languageSetting.xml.items)
            {
                ESF_Xml_BuildAssetBundle_Lang t = new ESF_Xml_BuildAssetBundle_Lang();
                t.classify = key.classify;
                List<string> tempPaths = new List<string>();
                string p = string.Empty;
                if (assetBundleRoots != null)
                {
                    foreach (string r in assetBundleRoots)
                    {
                        p = Path.Combine(r, key.classify.ESF_FirstAttribute<ESF_FolderNameAttribute>().folderName).ESF_TransPathSeparatorCharToUnityChar();
                        if (!tempPaths.Contains(p))
                        {
                            tempPaths.Add(p);
                        }
                    }
                }
                t.assetBundleFolders = tempPaths.ToArray();
                if (key.isSupport)
                {
                    if (t.classify == ESF_Logic.languageSetting.xml.defaultAlternateLanguage)
                    {
                        defaultAlternateLang = t;
                    }
                    sup.Add(t);
                }
                else
                {
                    unSup.Add(t);
                }
            }
        }
        supportLangs = sup.ToArray();
        unSupportLangs = unSup.ToArray();
    }

    /// <summary>
    /// 获得相对于语言包的路径
    /// </summary>
    /// <param name="_path">路径</param>
    /// <returns>相对于语言包的路径</returns>
    public string GetRelativeLangPath(string _path)
    {
        enESF_AllLanguageClassify lan = defaultAlternateLang.classify;
        string rootFolder = string.Empty;
        return GetRelativeLangPath(_path,ref lan,ref rootFolder);
    }

    /// <summary>
    /// 获得相对于语言包的路径
    /// </summary>
    /// <param name="_path">路径</param>
    /// <param name="_lan">语言</param>
    /// <param name="_rootFolder">根目录</param>
    /// <returns>相对于语言包的路径</returns>
    public string GetRelativeLangPath(string _path,ref enESF_AllLanguageClassify _lan,ref string _rootFolder)
    {
        bool isBreak = false;
        _path = _path.ESF_TransPathSeparatorCharToUnityChar();
        foreach (var lan in ESF_Logic.assetBundleBuild.xml.supportLangs)
        {
            foreach (var abf in lan.assetBundleFolders)
            {
                if (_path.StartsWith(abf))
                {
                    _path = _path.Replace(abf, "");
                    _path = _path.ESF_ForceRelativePath();
                    _lan = lan.classify;
                    _rootFolder = Path.GetDirectoryName(abf).ESF_TransPathSeparatorCharToUnityChar();
                    isBreak = true;
                    break;
                }
            }
            if (isBreak)
            {
                break;
            }
        }
        return _path;
    }

    /// <summary>
    /// 获得根路径枚举与路径映射
    /// </summary>
    /// <returns>Key:枚举名称,Value:路径</returns>
    public Dictionary<string, string> GetRootFolderEnumPathMaping()
    {
        Dictionary<string, string> enNames = new Dictionary<string, string>();
        foreach (var key in assetBundleRoots)
        {
            enNames[key.ESF_OnlyCNUAndOtherReplaceU().ToUpper()] = key;
        }
        return enNames;
    }

    /// <summary>
    /// 获得指定路径的相对根路径枚举名称
    /// </summary>
    /// <param name="_path">指定路径</param>
    /// <param name="_relativePath">相对根路径</param>
    /// <returns>根路径枚举名称</returns>
    public bool GetRootFolderEnumName(string _path,out string _enName,out string _rootPath,out string _relativePath)
    {
        bool result = false;
        _enName = string.Empty;
        _rootPath = string.Empty;
        _relativePath = string.Empty;
        Dictionary<string, string> enNames = GetRootFolderEnumPathMaping();
        string path = _path.ESF_TransPathSeparatorCharToUnityChar().ToLower();
        foreach (var key in enNames)
        {
            if (path.StartsWith(key.Value.ESF_TransPathSeparatorCharToUnityChar().ToLower()))
            {
                _enName = key.Key;
                _rootPath = key.Value;
                _relativePath = Regex.Replace(_path, key.Value, "", RegexOptions.IgnoreCase).ESF_ForceRelativePath();
            }
        }
        return result;
    }
}

/// <summary>
/// AssetBundle资源包生成语言包
/// </summary>
[XmlRoot("ESF_Xml_BuildAssetBundle_Lang")]
public class ESF_Xml_BuildAssetBundle_Lang : AbsESF_Xml
{
    /// <summary>
    /// 语言类别
    /// </summary>
    [XmlElement("语言类别")]
    [ESF_NoDuplicateValue()]
    [ReadOnly()]
    public enESF_AllLanguageClassify classify;
    /// <summary>
    /// AssetBundle目录
    /// </summary>
    [XmlElement("AssetBundle目录")]
    [ESF_GUIBrower()]
    [ReadOnly()]
    public string[] assetBundleFolders;
}

/// <summary>
/// AssetBundle资源包生成语言包
/// </summary>
[XmlRoot("ESF_Xml_DonwLoadWindow_Lang")]
public class ESF_Xml_DonwLoadWindow_Lang : AbsESF_Xml
{
    /// <summary>
    /// 语言类别
    /// </summary>
    [XmlElement("语言类别")]
    [ESF_NoDuplicateValue()]
    public enESF_AllLanguageClassify classify;
    /// <summary>
    /// AssetBundle目录
    /// </summary>
    [XmlElement("下载资源窗口目录")]
    [ESF_GUIOperation()]
    [ESF_PathSelect(enESF_PathSelectCatalogClassify.InternalAssets, enESF_PathSelectAssetClassify.Folder)]
    public string[] assetBundleFolders;
}
#endif

